package com.jzo2o.customer.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.api.publics.SmsCodeApi;
import com.jzo2o.common.enums.SmsBussinessTypeEnum;
import com.jzo2o.common.expcetions.BadRequestException;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.common.utils.DateUtils;
import com.jzo2o.common.utils.StringUtils;
import com.jzo2o.common.utils.UserContext;
import com.jzo2o.customer.config.SecurityConfig;
import com.jzo2o.customer.enums.CertificationStatusEnum;
import com.jzo2o.customer.mapper.AgencyCertificationAuditMapper;
import com.jzo2o.customer.model.domain.*;
import com.jzo2o.customer.model.dto.request.AgencyCertificationAuditAddReqDTO;
import com.jzo2o.customer.model.dto.request.InstitutionRegisterReqDTO;
import com.jzo2o.customer.model.dto.response.ResponseDTO;
import com.jzo2o.customer.service.ICertificationAuditService;
import com.jzo2o.customer.service.IRegisterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
@Slf4j
public class CertificationAuditService extends ServiceImpl<BaseMapper<AgencyCertification>, AgencyCertification> implements ICertificationAuditService {

    @Resource
    private   AgencyCertificationAuditMapper  agencyCertificationAuditMapper;



    /**
     * 机构认证申请
     *
     * @param agencyCertificationAuditAddReqDTO
     */
    @Override

    public void certificationAudit(AgencyCertificationAuditAddReqDTO agencyCertificationAuditAddReqDTO) {
        // 获取用户ID
        Long userId = UserContext.currentUserId();
        if (userId == null) {
            throw new BadRequestException("用户ID为空");
        }

        log.info("用户ID: {}", userId);

        // 属性转换
        AgencyCertification agencyCertification = BeanUtils.toBean(agencyCertificationAuditAddReqDTO, AgencyCertification.class);

        if (ObjectUtil.isEmpty(agencyCertification)) {
            throw new BadRequestException("参数不能为空");
        }

        // 查询当前用户的审核记录
        QueryWrapper<AgencyCertificationAudit> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("serve_provider_id", userId);
        AgencyCertificationAudit existingAudit = agencyCertificationAuditMapper.selectOne(queryWrapper);

        log.info("查询到的现有审核记录: {}", existingAudit);

        if (existingAudit == null) {
            // 如果没有现有的审核记录，则新增一条
            AgencyCertificationAudit audit = BeanUtils.toBean(agencyCertificationAuditAddReqDTO, AgencyCertificationAudit.class);

            audit.setId(userId);
            audit.setAuditStatus(CertificationStatusEnum.INIT.getStatus());
            audit.setCertificationStatus(CertificationStatusEnum.PROGRESSING.getStatus());
            audit.setServeProviderId(userId);
            audit.setCreateTime(DateUtils.now());

            // 保存审计记录
            agencyCertificationAuditMapper.insert(audit);

            // 更新AgencyCertification对象
            agencyCertification.setId(userId);
            agencyCertification.setCertificationTime(DateUtils.now());
            agencyCertification.setCertificationStatus(CertificationStatusEnum.PROGRESSING.getStatus());

            // 保存certification对象
            baseMapper.insert(agencyCertification);

            log.info("初始化状态处理完成");
        } else if (CertificationStatusEnum.FAIL.getStatus() == existingAudit.getCertificationStatus()) {
            // 如果有现有的审核记录且状态为驳回，则删除旧的审核记录和认证记录
            agencyCertificationAuditMapper.deleteById(existingAudit.getId());
            baseMapper.deleteById(userId);

            // 更新新数据
            AgencyCertificationAudit audit = BeanUtils.toBean(agencyCertificationAuditAddReqDTO, AgencyCertificationAudit.class);

            audit.setId(userId);
            audit.setAuditStatus(CertificationStatusEnum.INIT.getStatus());
            audit.setCertificationStatus(CertificationStatusEnum.PROGRESSING.getStatus());
            audit.setServeProviderId(userId);
            audit.setCreateTime(DateUtils.now());

            // 保存新的审计记录
            agencyCertificationAuditMapper.insert(audit);

            // 更新AgencyCertification对象
            agencyCertification.setId(userId);
            agencyCertification.setCertificationTime(DateUtils.now());
            agencyCertification.setCertificationStatus(CertificationStatusEnum.PROGRESSING.getStatus());

            // 保存certification对象
            baseMapper.insert(agencyCertification);

            log.info("驳回状态处理完成");
        } else {
            throw new BadRequestException("不支持的状态");
        }
    }
//        //获取用户id
//        Long userId = UserContext.currentUserId();
//
//        //属性转换
//        AgencyCertification agencyCertification = BeanUtils.toBean(agencyCertificationAuditAddReqDTO, AgencyCertification.class);
//
//        if (ObjectUtil.isEmpty(agencyCertification)) {
//            throw new BadRequestException("参数不能为空");
//        }
//
//// 创建WorkerCertification对象
//        AgencyCertificationAudit audit = BeanUtils.toBean(agencyCertificationAuditAddReqDTO, AgencyCertificationAudit.class);
//
//// 创建WorkerCertificationAudit对象
//
//        audit.setId(userId);
//        audit.setAuditStatus(CertificationStatusEnum.INIT.getStatus());
//        audit.setCertificationStatus(CertificationStatusEnum.PROGRESSING.getStatus());
//        audit.setServeProviderId(UserContext.currentUserId());
//        audit.setCreateTime(DateUtils.now());
//
//// 保存审计记录
//        agencyCertificationAuditMapper.insert(audit);
//
//// 更新WorkerCertification对象
//        agencyCertification.setId(userId);
//        agencyCertification.setCertificationTime(DateUtils.now());
//        agencyCertification.setCertificationStatus(CertificationStatusEnum.PROGRESSING.getStatus());
//        agencyCertification.setId(UserContext.currentUserId());
//        // 保存certification对象
//        baseMapper.insert(agencyCertification);
//
//    }

    /**
     * 机构认证申请驳回原因
     */
    @Override
    public ResponseDTO getRejectReason() {
        //获取用户id
        Long userId = UserContext.currentUserId();
        //根据用户id查询
        AgencyCertificationAudit agencyCertificationAudit = agencyCertificationAuditMapper.selectById(userId);
        if (ObjectUtil.isEmpty(agencyCertificationAudit)) {
            throw new BadRequestException("参数不能为空");
        }
        //封装对象 返回数据
        ResponseDTO responseDTO = new ResponseDTO();
        responseDTO.setRejectReason(agencyCertificationAudit.getRejectReason());
        return responseDTO;

    }
}
